package scau.mis4.chanyulin.service;

import com.alibaba.fastjson.JSONObject;
import scau.mis4.chanyulin.tools.utils.HttpUtils;
import scau.mis4.chanyulin.view.Global;
import java.io.IOException;

/**
 * Created by cyl on 2017/12/5.
 * 获取室外温度
 */
public class OutdoorTempService {

    /**
     * 获取实时室外温度
     * @param city
     * @return
     */
    public static Integer getOutTemp(String city) {
        // api地址为http://www.sojson.com/open/api/weather/json.shtml?city=城市名
        String url = "http://" + Global.OUT_API_URL + city;
        String resJs = "";
        try {
            resJs = HttpUtils.httpGet(url, 1500, 1500);
        } catch (IOException e) {
            System.out.println("[室外温度API] 连接失败或连接超时");
            e.printStackTrace();
        }
        JSONObject jsObj = JSONObject.parseObject(resJs);
        try {
            String temperStr = jsObj.getJSONObject("data").getString("wendu");
            Integer temper = Integer.valueOf(temperStr);
            return temper;
        } catch (NullPointerException e) {
            System.out.println("[室外温度API] 数据获取出现异常");
            return null;
        }
    }

}
